#include "stdafx.h"
#include "IBasicResourceMgr.h"
#include "BasicSkinTheme.h"

namespace BasicSkinView
{

	IBasicResourceMgr::IBasicResourceMgr()
	{
	}

	IBasicResourceMgr::~IBasicResourceMgr()
	{
	}

	Image* IBasicResourceMgr::ILoadResImage( const wchar_t* szImage )
	{
		IInitIResourceMgr();
		return CBasicSkinThemeMgr::Theme_LoadImage(szImage);
	}

	Image* IBasicResourceMgr::ILoadThemeBkgnd()
	{
		return ILoadResImage(L"MainBkgnd.png");
	}

	Image* IBasicResourceMgr::ILoadThemeBorder()
	{
		return ILoadResImage(L"border2.png");
	}

	void IBasicResourceMgr::IInitIResourceMgr()
	{
	}

	bool IBasicResourceMgr::Theme_LoadRes( const wchar_t* szFile )
	{
		return CBasicSkinThemeMgr::Theme_LoadRes(szFile);
	}

	HANDLE IBasicResourceMgr::Theme_LoadResEx( const wchar_t* szFile )
	{
		return CBasicSkinThemeMgr::Theme_LoadResEx(szFile);
	}

	Image* IBasicResourceMgr::Theme_LoadImage( const wchar_t* szIm )
	{
		return CBasicSkinThemeMgr::Theme_LoadImage(szIm);
	}

	Image* IBasicResourceMgr::Theme_LoadImageEx( HANDLE handle,const wchar_t* szIm )
	{
		CBasicThemePackage* pPackage = (CBasicThemePackage*)handle;
		return pPackage->LoadImage(szIm);
	}

	void IBasicResourceMgr::Theme_Close()
	{
		CBasicSkinThemeMgr::Theme_Close();
	}

	void IBasicResourceMgr::Theme_CloseEx( HANDLE handle )
	{
		CBasicThemePackage* pPackage = (CBasicThemePackage*)handle;
		if (pPackage)
		{
			pPackage->Close();
			delete pPackage;
		}
	}

	void IBasicResourceMgr::Theme_DrawFrameBorder( Graphics* gh,LPRECT lpRect,Image* pBorder )
	{
		if (pBorder)
		{
			int cx = lpRect->right - lpRect->left;
			int cy = lpRect->bottom - lpRect->top;
			int x = lpRect->left;
			int y = lpRect->top;

			int cxBorder = 10;

			//Top
			TextureBrush brTop(pBorder,WrapMode::WrapModeTile,RectF(cxBorder,0,200,cxBorder));
			brTop.TranslateTransform(x+cxBorder,y);
			gh->FillRectangle(&brTop,x+cxBorder,y,cx-cxBorder-cxBorder,cxBorder);
			//Left
			TextureBrush brLeft(pBorder,WrapMode::WrapModeTile,RectF(0,cxBorder,cxBorder,140));
			brLeft.TranslateTransform(x,y+cxBorder);
			gh->FillRectangle(&brLeft,x,y+cxBorder,cxBorder,cy-cxBorder-cxBorder);
			//Right
			TextureBrush brRight(pBorder,WrapMode::WrapModeTile,RectF(pBorder->GetWidth()-cxBorder,cxBorder,cxBorder,140));
			brRight.TranslateTransform(x+cx-cxBorder,y+cxBorder);
			gh->FillRectangle(&brRight,x+cx-cxBorder,y+cxBorder,cxBorder,cy-cxBorder-cxBorder);
			//Bottom
			TextureBrush brBottom(pBorder,WrapMode::WrapModeTile,RectF(cxBorder,pBorder->GetHeight()-cxBorder,200,cxBorder));
			brBottom.TranslateTransform(x+cxBorder,y+cy-cxBorder);
			gh->FillRectangle(&brBottom,x+cxBorder,y+cy-cxBorder,cx-cxBorder-cxBorder,cxBorder);

			//Left Top Corner
			gh->DrawImage(pBorder,RectF(x,y,cxBorder,cxBorder),0,0,cxBorder,cxBorder,UnitPixel);

			//Right Top Corner
			gh->DrawImage(pBorder,RectF(x+cx-cxBorder,y,cxBorder,cxBorder),pBorder->GetWidth()-cxBorder,0,cxBorder,cxBorder,UnitPixel);

			//Left Bottom Corner
			gh->DrawImage(pBorder,RectF(x,y+cy-cxBorder,cxBorder,cxBorder),0,pBorder->GetHeight()-cxBorder,cxBorder,cxBorder,UnitPixel);

			//Right Bottom Corner
			gh->DrawImage(pBorder,RectF(x+cx-cxBorder,y+cy-cxBorder,cxBorder,cxBorder),pBorder->GetWidth()-cxBorder,pBorder->GetHeight()-cxBorder,cxBorder,cxBorder,UnitPixel);

			POINT ptA[] = {{0,0},{0,1},{1,0},{2,0},{0,2}};
			SIZE sz;

			sz.cx = lpRect->right-lpRect->left;
			sz.cy = lpRect->bottom-lpRect->top;
			SolidBrush br(Color(0,0,0,0));

			for(int i=0;i<sizeof(ptA)/sizeof(ptA[0]);i++)
			{
				//Top Left
				gh->FillRectangle(&br,ptA[i].x,ptA[i].y,1,1);
				//Top Right
				gh->FillRectangle(&br,sz.cx-1-ptA[i].x,ptA[i].y,1,1);

				//Left Bottom
				gh->FillRectangle(&br,ptA[i].x,sz.cy - 1 -ptA[i].y,1,1);

				//Right Bottom
				gh->FillRectangle(&br,sz.cx-1-ptA[i].x,sz.cy - 1 -ptA[i].y,1,1);
			}
		}
	}

	bool IBasicResourceMgr::Theme_LoadFileRes( HANDLE handle,const wchar_t* szPath,char** lpDataAddress,int* nLen )
	{
		return CBasicSkinThemeMgr::Theme_LoadFileRes(handle,szPath,lpDataAddress,nLen);
	}

	bool IBasicResourceMgr::Theme_LoadTheme( const wchar_t* szResFile )
	{
		return CBasicSkinThemeMgr::Theme_LoadTheme(szResFile);
	}

	bool IBasicResourceMgr::Theme_LoadThemeByColor( COLORREF cl )
	{
		return CBasicSkinThemeMgr::Theme_LoadThemeByColor(cl);
	}

	void IBasicResourceMgr::Theme_SetAlpha( int nValue )
	{
		CBasicSkinThemeMgr::SetAlpha(nValue);
	}

	int IBasicResourceMgr::Theme_GetAlpha()
	{
		return CBasicSkinThemeMgr::GetAlpha();
	}

	HANDLE IBasicResourceMgr::Theme_OpenTheme()
	{
		return CBasicSkinThemeMgr::Theme_OpenTheme();
	}

	void IBasicResourceMgr::Theme_RenderSkinBkgnd( HANDLE hThem,Graphics* gh,int x,int y,int cx,int cy )
	{
		CBasicSkinThemeMgr::Theme_RenderSkinBkgnd(hThem,gh,x,y,cx,cy);
	}

	void IBasicResourceMgr::Theme_RenderMainSkinBkgnd( HANDLE hThem,Graphics* gh,int x,int y,int cx,int cy )
	{
		CBasicSkinThemeMgr::Theme_RenderMainSkinBkgnd(hThem,gh,x,y,cx,cy);
	}

	void IBasicResourceMgr::Theme_CloseTheme( HANDLE hTheme )
	{
		CBasicSkinThemeMgr::Theme_CloseTheme(hTheme);
	}

	void IBasicResourceMgr::Theme_DrawBorderImageToRect( Graphics* pGraphics,Image* pIm,LPRECT lpRect )
	{
		if (pIm)
		{
			int cxBorder = 8;
			int icx = pIm->GetWidth();
			int icy = pIm->GetHeight();
			int cx = lpRect->right - lpRect->left;
			int cy = lpRect->bottom - lpRect->top;

			int xMin = min(icx,cx)/2;
			int yMin = min(icy,cy)/2;

			int x = lpRect->left;
			int y = lpRect->top;
			Color cl(0,0,0,0);
			SolidBrush brClear(cl);

			//Left Top
			pGraphics->DrawImage(pIm,RectF(lpRect->left,lpRect->top,xMin,yMin),0,0,xMin,yMin,UnitPixel);
			//Left Bottom
			pGraphics->DrawImage(pIm,RectF(lpRect->left,lpRect->top+cy-yMin,xMin,yMin),0,icy-yMin,xMin,yMin,UnitPixel);
			//Right Top
			pGraphics->DrawImage(pIm,RectF(lpRect->left+cx-xMin,lpRect->top,xMin,yMin),icx-xMin,0,xMin,yMin,UnitPixel);
			//Right Bottom 
			pGraphics->DrawImage(pIm,RectF(lpRect->left+cx-xMin,lpRect->top+cy-yMin,xMin,yMin),icx-xMin,icy-yMin,xMin,yMin,UnitPixel);

			//static POINT pt[] = {{0,0},{1,0},{0,1},{2,0},{0,2}
			//};
			//for(int i=0;i<sizeof(pt)/sizeof(pt[0]);i++)
			//{
			//	if (m_pMemDC)
			//	{
			//		m_pMemDC->ClearRect(x+pt[i].x+2,y+pt[i].y+2,1,1);
			//		m_pMemDC->ClearRect(x+cx-pt[i].x-3,y+pt[i].y+2,1,1);
			//		m_pMemDC->ClearRect(x+pt[i].x+2,y+cy-pt[i].y-3,1,1);
			//		m_pMemDC->ClearRect(x+cx-pt[i].x-3,y+cy-pt[i].y-3,1,1);
			//	}
			//}

			int xFH = cx-xMin*2;
			int yFV = cy-yMin*2;

			if (xFH > 0)
			{
				TextureBrush br(pIm,WrapModeTile,RectF(xMin,0,1,cxBorder));
				br.TranslateTransform(lpRect->left+xMin,lpRect->top);
				pGraphics->FillRectangle(&br,lpRect->left+xMin,lpRect->top,xFH,cxBorder);
				TextureBrush br2(pIm,WrapModeTile,RectF(xMin,icy-cxBorder,1,cxBorder));
				br2.TranslateTransform(lpRect->left+xMin,lpRect->bottom-cxBorder);
				pGraphics->FillRectangle(&br2,lpRect->left+xMin,lpRect->bottom-cxBorder,xFH,cxBorder);
			}
			if (yFV > 0)
			{
				TextureBrush br(pIm,WrapModeTile,RectF(0,yMin,cxBorder,1));
				br.TranslateTransform(lpRect->left,lpRect->top+yMin);
				pGraphics->FillRectangle(&br,lpRect->left,lpRect->top+yMin,cxBorder,yFV);
				TextureBrush br2(pIm,WrapModeTile,RectF(icx-cxBorder,yMin,cxBorder,1));
				br2.TranslateTransform(lpRect->right-cxBorder,lpRect->top+yMin);
				pGraphics->FillRectangle(&br2,lpRect->right-cxBorder,lpRect->top+yMin,cxBorder,yFV);
			}
		}
	}

	bool IBasicResourceMgr::Theme_CompressRes( const wchar_t* szInputFolderPath,const wchar_t* szOutputFilePath )
	{
		return CBasicSkinThemeMgr::CompressRes(szInputFolderPath,szOutputFilePath);
	}

	void IBasicResourceMgr::Theme_DrawTitleImageToRect( Graphics* pGraphics,Image* pIm,LPRECT lpRect,int nFillType )
	{
		CBasicSkinThemeMgr::Theme_DrawTitleImageToRectEx(pGraphics,pIm,lpRect,nFillType);
	}

	Image* IBasicResourceMgr::Theme_GetBgImage( HANDLE hThem,COLORREF& clr )
	{
		return NULL;
	}

	COLORREF WINAPI IBasicResourceMgr::Theme_GetCurThemeTextColor()
	{
		return CBasicSkinThemeMgr::GetCurThemeTextColor();
	}

	void WINAPI IBasicResourceMgr::Theme_DrawCurTitleBackground( Graphics* pGh,LPRECT pRect,int nDefH )
	{
		CBasicSkinThemeMgr::DrawCurTitleBackground(pGh,pRect,nDefH);
	}
}